<style lang="less">
@import '../style.less';
</style>

<template>
	<div class="classification">
		<tree-ex
			:data="evidenClasses"
			:default-expanded-keys="defaultExpandedKeys"
			:on-node-click="onClassClick"
			:on-operation="onOperation"
			:operations="operations"
			:props="{ children: 'subClasses', label: 'name' }"
			tree-title="凭证模版分类"
		/>
		<dialog-ex :form="form" :init="currentClass" :mode="dialogMode" title="凭证模版分类" v-if="dialogMode"/>
	</div>
</template>

<script>
/**
 * 交易分类
 */
import { mapGetters, mapActions, mapMutations } from 'vuex'
import treeEx from '@/components/tree/treeEx'
import dialogEx from '@/components/dialog/dialogEx'
import pageConfig from '../config'

export default {
  name: 'evidenClass',
  components: {
    'tree-ex': treeEx,
    'dialog-ex': dialogEx
  },
  data() {
    return {
      dialogMode: null,
      currentClass: null,
      defaultExpandedKeys: null,
      operations: pageConfig.evidence.operations,
      form: {
        items: pageConfig.evidence.classFormItems,
        default: { enabled: true },
        actions: {
          cancel: () => (this.dialogMode = null),
          confirm: (data, mode) => {
            this.onConfirm(data, mode)
            this.dialogMode = null
          }
        }
      }
    }
  },
  mounted() {
    this.getEvidenceClasses()
  },
  computed: {
    ...mapGetters(['evidenClasses'])
  },
  methods: {
    ...mapActions([
      'getEvidenceClasses',
      'saveEvidenceClass',
      'removeEvidenceClass'
    ]),
    onClassClick(currentClass) {
      this.currentClass = currentClass
      console.log('onClassClick', currentClass)
    },
    onOperation(e) {
      if (e.prop === 'refresh') {
        this.currentClass = null
        this.getEvidenceClasses()
      } else if (e.prop === 'add') {
        this.dialogMode = 'add'
        this.showDialog = true
      } else if (e.prop === 'edit') {
        this.dialogMode = 'edit'
        this.showDialog = true
      } else if (
        e.prop === 'delete' &&
        this.currentClass &&
        this.currentClass.id
      ) {
        this.removeEvidenceClass(this.currentClass.id)
        this.currentClass = null
      }
      console.log(e)
    },
    onConfirm(data, mode) {
      if (mode === 'add' && this.currentClass) {
        data.classParentId = this.currentClass.id
        this.defaultExpandedKeys = [this.currentClass.id]
        this.currentClass = null
      }
      this.saveEvidenceClass(data)
    }
  }
}
</script>